﻿using UnityEngine;
using UnityEngine.EventSystems;

public class MyInputSystem : MonoBehaviour
{
    [System.Serializable]
    public struct MouseInfo
    {
        public Vector3 position;
        public Vector3 worldPosition;
        public bool pressing;
        public float pressTime;
        public GameObject castObject;
        public bool isOnUI;
        public bool longPressing;
        public void Clear()
        {
            pressTime = 0;
            pressing = false;
            castObject = null;
            isOnUI = false;
            longPressing = false;
        }
    }
    public static MyInputSystem Instance { get; private set; }

    public float LongPressTimeThreshold = 0.5f;
    public float LongPressPosDeltaThreshold = 10f;
    public float ClickTimeThreshold = 0.3f;
    public float ClickPosDeltaThreshold = 10f;

    public MouseInfo curMouse;
    [HideInInspector]
    public MouseInfo prevMouse;
    [HideInInspector]
    public MouseInfo pressStartMouse;
    [HideInInspector]
    public bool isZooming;
    [HideInInspector]
    public float zoomDelta;
    [HideInInspector]
    public Camera mainCamera;
    public Transform mainCameraTrans;
    
    void Start()
    {
        Instance = this;
        mainCamera = Camera.main;
        mainCameraTrans = mainCamera.transform;
    }
    void Update()
    {
        prevMouse = curMouse;

#if UNITY_EDITOR || UNITY_STANDALONE
        curMouse.Clear();
        curMouse.position = Input.mousePosition;
        curMouse.worldPosition = mainCamera.ScreenToWorldPoint(curMouse.position);
        if (Input.GetMouseButton(0))
        {
            curMouse.pressing = true;
            curMouse.isOnUI = EventSystem.current.IsPointerOverGameObject();
            if (!curMouse.isOnUI)
            {
                Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
                RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);

                if (hit.collider != null)
                {
                    curMouse.castObject = hit.collider.gameObject;
                }
            }
            
            if (prevMouse.pressing)
            {
                curMouse.pressTime = prevMouse.pressTime + Time.deltaTime;
                if (curMouse.pressTime > LongPressTimeThreshold && (curMouse.position - pressStartMouse.position).magnitude < LongPressPosDeltaThreshold)
                {
                    if (!prevMouse.longPressing)
                    {
                        MessageManager<GameObject>.Instance.Invoke(ClientEvent.StartLongPressObject, curMouse.castObject);
                    }
                    curMouse.longPressing = true;
                }
            }

            if (Input.GetMouseButtonDown(0))
            {
                pressStartMouse = curMouse;
            }
        }
        else
        {
            // mouse up
            if (prevMouse.pressing && prevMouse.pressTime < ClickTimeThreshold && (pressStartMouse.position - curMouse.position).magnitude < ClickPosDeltaThreshold)
            {
                // 点击了一个物体
                MessageManager<GameObject>.Instance.Invoke(ClientEvent.ClickObject, prevMouse.castObject);
            }
        }

        float scroll = Input.GetAxis("Mouse ScrollWheel");
        isZooming = scroll != 0;
        zoomDelta = - scroll * 10;
#elif UNITY_ANDROID
//if (!Input.GetMouseButton(0))
//        {
//            hasStart = false;
//        }
//        // 检测触摸输入
//        if (Input.GetMouseButtonDown(0) && Input.touchCount == 1 && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
//        {
//            touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//            hasStart = true;
//        }
//        // 检测触摸移动
//        if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved && hasStart)
//        {
//            Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
//            //Camera.main.transform.position += direction;
//            Vector3 newPosition = Camera.main.transform.position + direction;
//            newPosition.x = Mathf.Clamp(newPosition.x, minX, maxX);
//            newPosition.y = Mathf.Clamp(newPosition.y, minY, maxY);
//            Camera.main.transform.position = newPosition;
//        }
//        // 检测触摸缩放
//        if (Input.touchCount == 2)
//        {
//            Touch touchZero = Input.GetTouch(0);
//            Touch touchOne = Input.GetTouch(1);

//            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
//            Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

//            float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
//            float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

//            float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

//            Camera.main.orthographicSize += deltaMagnitudeDiff * 0.01f;
            
//        }
#endif
    }
}